package main

import (
	grpcrecovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
	"log"
	global2 "longmen/server/config/global"
	"longmen/server/internal/rpc/games/fruit_machine"
	"longmen/server/internal/rpc/games/server"
	middleware2 "longmen/server/internal/rpc/middleware"
	"longmen/server/pkg/common/app"
	"longmen/server/pkg/common/app/etcd"
	appServer "longmen/server/pkg/common/app/server"
	_const "longmen/server/pkg/common/helper/const"
	"longmen/server/pkg/db/redis/cache"
	"longmen/server/pkg/grpc_client"
	pb "longmen/server/pkg/protobuf"
)

func main() {
	global2.Initialize("../../../config/app.properties")
	//初始化MQ
	//global2.InitMQ()
	//global2.InitPublisher()
	//设置aes加密key
	cache.SetAESKey()
	err := global2.InitElasticSearch()
	if err != nil {
		log.Fatalf("%s", err.Error())
	}
	global2.AppName = "webapi_rpc_games"
	grpc_client.InitGrpc()
	limit := middleware2.NewTencentImRequestLimit()

	//metrics := prometheus.NewGrpcMetrics()
	grpcSrv := appServer.New(
		appServer.Network("tcp"),
		appServer.Address(":"+global2.Config.GetString("grpc.game_server_port")),
		appServer.UnaryServerInterceptor(
			grpcrecovery.UnaryServerInterceptor(appServer.RecoveryOption()...),
			middleware2.TokenValid,
			limit.RequestLimit,
			//grpczap.UnaryServerInterceptor(global.ZapLogger),
			//metrics.GrpcMetrics.UnaryServerInterceptor(),
		),
	)
	grpcGameServer := &server.GameServer{}
	grpcGameServer.FruitGame = fruit_machine.NewFruits()
	pb.RegisterGameServiceServer(grpcSrv, grpcGameServer)
	app := app.NewApp(
		app.Version(global2.GetMicroserviceVersion()),
		app.Name(global2.GetMicroserviceName(_const.GRPC_GAMES_SERVER_NAME_KEY)),
		app.Register(etcd.NewEtcdRegister(global2.GetEtcdCli())),
		app.Server(grpcSrv),
	)
	server.NewGamesServer()

	if err := app.Start(); err != nil {
		log.Fatalf("init account server is error: %+v \n", err)
	}
}
